package net.lainiao.manager.control;

import com.github.pagehelper.PageInfo;
import net.lainiao.base.model.ShiroRole;
import net.lainiao.base.model.ShiroUser;
import net.lainiao.base.model.ShiroUserRole;
import net.lainiao.base.model.exp.ShiroUserRoleExp;
import net.lainiao.service.service.ShiroRoleService;
import net.lainiao.service.service.ShiroUserRoleService;
import net.lainiao.service.service.ShiroUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/1/22.
 */
@Controller
@RequestMapping(value = "/user")
public class UserControl {
    @Resource
    private ShiroUserService shiroUserService;
    @Resource
    private ShiroUserRoleService shiroUserRoleService;
    @Resource
    private ShiroRoleService shiroRoleService;
    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap){
        return index("1",modelMap);
    }

    @RequestMapping(value = "/index/{page}")
    public String index(@PathVariable String page,ModelMap modelMap) {
        int pageIndex=Integer.parseInt(page);
        PageInfo<ShiroUser> pageInfo=shiroUserService.getModelsForPage(pageIndex);
        modelMap.put("models", pageInfo.getList());
        modelMap.put("pageIndex", pageIndex);
        modelMap.put("pageTotal", pageInfo.getPages());
        return "/user/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "/user/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(@ModelAttribute ShiroUser user, ModelMap modelMap){
        if(shiroUserService.save(user)>0){
            return "redirect:/user/index";
        }
        modelMap.put("mess","保存失败");
        modelMap.put("net.lainiao.zhihu.model",user);
        return "/user/add";
    }

    @RequestMapping(value = "/edit/{userId}",method = RequestMethod.GET)
    public String edit(@PathVariable String userId,ModelMap modelMap)
    {
        int uid=Integer.parseInt(userId);
        ShiroUser user=shiroUserService.getModelForId(uid);
        modelMap.put("net.lainiao.zhihu.model",user);
        return "/user/edit";
    }

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public String edit(@ModelAttribute ShiroUser user,ModelMap modelMap){
        if(shiroUserService.save(user)>0){
            return "redirect:/user/index";
        }
        modelMap.put("net.lainiao.zhihu.model",user);
        modelMap.put("mess","保存失败");
        return "/user/edit";
    }

    @RequestMapping(value = "del/{idstr}")
    public String del(@PathVariable String idstr,ModelMap modelMap)
    {
        int id=Integer.parseInt(idstr);
        if(shiroUserService.del(id)>0){
            return "redirect:/user/index";
        }
        modelMap.put("mess","删除失败");
        return "/user/index";
    }

    @RequestMapping(value = "/role/index/{userId}",method = RequestMethod.GET)
    public String role_index(@PathVariable String userId,ModelMap modelMap){
        int uid=Integer.parseInt(userId);
        List<ShiroUserRoleExp> roleList=shiroUserRoleService.getUserRoleExpByUserId(uid);
        modelMap.put("models",roleList);
        return "/user/role/index";
    }

    @RequestMapping(value ="/role/add/{userId}",method = RequestMethod.GET)
    public String role_add(@PathVariable String userId,ModelMap modelMap){
        modelMap.put("userId",userId);
        List<ShiroRole> roles=shiroRoleService.getAllRoles();
        modelMap.put("roles",roles);
        return "/user/role/add";
    }

    @RequestMapping(value = "/role/add",method = RequestMethod.POST)
    public String role_add(ModelMap modelMap, @ModelAttribute ShiroUserRole shiroUserRole){
        int count=shiroUserRoleService.getCountByUserIdRoleId(shiroUserRole.getUserId(),shiroUserRole.getRoleId());
        if(count>0){
            modelMap.put("userId",shiroUserRole.getUserId());
            modelMap.put("net.lainiao.zhihu.model",shiroUserRole);
            modelMap.put("mess","该角色已经对于该用户授权过了");
            List<ShiroRole> roles=shiroRoleService.getAllRoles();
            modelMap.put("roles",roles);
            return "/user/role/add";
        }
        if(shiroUserRoleService.save(shiroUserRole)>0){
            return "redirect:/user/role/index/"+shiroUserRole.getUserId();
        }
        modelMap.put("userId",shiroUserRole.getUserId());
        modelMap.put("net.lainiao.zhihu.model",shiroUserRole);
        modelMap.put("mess","保存失败");
        List<ShiroRole> roles=shiroRoleService.getAllRoles();
        modelMap.put("roles",roles);
        return "/user/role/add";
    }

    @RequestMapping(value = "/role/del/{idstr}",method = RequestMethod.GET)
    public String delRole(@PathVariable String idstr,ModelMap modelMap){
        int id=Integer.parseInt(idstr);
        ShiroUserRole shiroUserRole=shiroUserRoleService.getModelById(id);
        if(shiroUserRoleService.del(id)>0){
            return "redirect:/user/role/index/"+shiroUserRole.getUserId();
        }
        List<ShiroUserRoleExp> roleList=shiroUserRoleService.getUserRoleExpByUserId(shiroUserRole.getUserId());
        modelMap.put("models",roleList);
        modelMap.put("mess","删除失败");
        return "/user/role/index";
    }

}
